//package com.chengyu.core.utils;
//
//package web;
//
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.sql.SQLException;
//import java.util.HashMap;
//import java.util.Map;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import com.alibaba.fastjson.JSON;
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
//
//import util.PayUtil;
//
//@WebServlet("/PayCallback")
//public class WechatPayCallback extends HttpServlet {
//    private static final long serialVersionUID = 1L;
//
//    public PayCallback() {
//        super();
//    }
//
//    protected void doGet(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
//        try {
//            System.out.println("Wechatpay-Timestamp:" + request.getHeader("Wechatpay-Timestamp"));
//            System.out.println("Wechatpay-Nonce:" + request.getHeader("Wechatpay-Nonce"));
//            System.out.println("Wechatpay-Signature:" + request.getHeader("Wechatpay-Signature"));
//            System.out.println("Wechatpay-Serial:" + request.getHeader("Wechatpay-Serial"));
//
//            Map<String, String> result = new HashMap<String, String>();
//            result.put("code", "FAIL");
//            StringBuilder signStr = new StringBuilder();
//            signStr.append(request.getHeader("Wechatpay-Timestamp")).append("\n");
//            signStr.append(request.getHeader("Wechatpay-Nonce")).append("\n");
//            BufferedReader br = request.getReader();
//            String str = null;
//            StringBuilder builder = new StringBuilder();
//            while ((str = br.readLine()) != null) {
//                builder.append(str);
//            }
//            System.out.println(builder.toString());
//            signStr.append(builder.toString()).append("\n");
//            //进行验签，确保请求来自微信
//            if (!PayUtil.signVerify(request.getHeader("Wechatpay-Serial"), signStr.toString(), request.getHeader("Wechatpay-Signature"))) {
//                System.out.println("PayCallback==>>sign error");
//                result.put("message", "sign error");
//                String json = JSON.toJSONString(result);
//                PrintWriter out = response.getWriter();
//                out.write(json);
//                return;
//            }
//            System.out.println("PayCallback==>>sign success");
//            //解密报文
//            String info = PayUtil.decryptOrder(builder.toString());
//            System.out.println(info);
//            ObjectMapper objectMapper = new ObjectMapper();
//            JsonNode node = objectMapper.readTree(info);
//            //可以解密出很多参数，具体见[官方文档](https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_5.shtml)
//            String outTradeNo = node.get("out_trade_no").toString().substring(1, node.get("out_trade_no").toString().length() - 1);
//            String bankType = node.get("bank_type").toString().substring(1, node.get("bank_type").toString().length() - 1);
//            result.put("code", "SUCCESS");
//            String json = JSON.toJSONString(result);
//            PrintWriter out = response.getWriter();
//            out.write(json);
//        } catch (IOException | SQLException e) {
//            e.printStackTrace();
//        } finally {
//            DBUtil.close(conn);
//        }
//    }
//
//    protected void doPost(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        doGet(request, response);
//    }
//}